package com.hx.setting.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
public class Context
{

	private static Context		context		= new Context();

	private static Properties	properties	= null;

	private ApplicationContext	applicationContext;				/*private String siteDomain = null;*/

	public static Context getInstance()
	{
		/*		if(context == null){
				context = new Context();
			}*/
		return context;
	}

	private Context()
	{
		Resource resource = null;
		try
		{
			DefaultResourceLoader defaultResourceLoader = new DefaultResourceLoader();
			resource = defaultResourceLoader.getResource( "config-context/framework.properties.xml" );
			properties = loadFile( resource.getFile() );
			//siteDomain = properties.getProperty("siteDomain");
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
	}

	private static Properties loadFile(File path) throws Exception
	{
		Properties prop = null;
		BufferedInputStream inBuff = null;
		try
		{
			inBuff = new BufferedInputStream( new FileInputStream( path ) );
			prop = new Properties();
			if ( path.getName().endsWith( ".xml" ) ) prop.loadFromXML( inBuff );
			else prop.load( inBuff );
		}
		finally
		{
			if ( inBuff != null )
			{
				inBuff.close();
			}
		}
		return prop;
	}

	/** 
	 * 获取配置
	 * @param key
	 * @return
	 */
	public String getEntryValue(String key)
	{
		return properties.getProperty( key );
	}

	public int getIntEntryValue(String key)
	{
		int answer = -1;
		String tmp = properties.getProperty( key );
		if ( tmp != null && tmp.length() > 0 )
		{
			try
			{
				answer = Integer.parseInt( tmp );
			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}
		}
		return answer;
	}

	/** 
	 * 
	 * @param key
	 * @return
	 */
	public String getEntryValue(String key, String defaultValue)
	{
		return properties.getProperty( key ,defaultValue );
	}

	public ApplicationContext getApplicationContext()
	{
		return applicationContext;
	}

	public void setApplicationContext(ApplicationContext applicationContext)
	{
		this.applicationContext = applicationContext;
	}

	public String getSiteDomain()
	{
		return properties.getProperty( "siteDomain" );
	}

	public void mergeProperties(Properties properties)
	{
		if ( properties != null && properties.size() > 0 )
		{
			if ( Context.properties == null )
			{
				Context.properties = new Properties();
			}
			Context.properties.putAll( properties );
		}
	}

	public String getExtjs4ResourceDomain()
	{
		return properties.getProperty( "extjs4ResourceDomain" );
	}
}
/* Honey v3.1 Test - version:7 - OriginalChecksum:Be790da2d9ccd377eecbb51050438a94c (do not edit this line) */
